<?php
	include '../sys_application.php';
	include '../func/customerInfo.php';
	include '../func/func.php';
	
	$sysApps = new sysApps();
	$sysApps->DBconnect();
	
	$email = $_POST['email'];
	$password = $_POST['password'];
	
	$sql = "SELECT * FROM mgm_users WHERE email = '".$email."'";
	$result = mysql_query($sql);
	$numrowcheck = mysql_num_rows($result);
	if ($numrowcheck > 0) {
			$row = mysql_fetch_array($result);
			$id= $row['id'];
			$fullname = $row['fullname'];
			$user_pwd = $row['password'];
			$user_type = $row['user_type'];
			$user_MGM_id = $row['user_MGM_id'];
			$acc_status = $row['acc_status'];
			
			if ($acc_status == 'I') {
				$loginresult = 4; // acc not verify
			}
			
			else if ($password == $user_pwd) {
				session_start();
				$_SESSION['id'] = $id;
				$_SESSION['fullname'] = $fullname;
				$_SESSION['user_type'] = $user_type;
				$_SESSION['user_MGM_id'] = $user_MGM_id;
				$_SESSION['login_id'] = $user_MGM_id;
				
				$sql2 = "UPDATE mgm_users SET last_login = NOW() WHERE email = '".$email."'";
				$query2 = mysql_query($sql2);
				$loginresult = 1; //pwd sama
				
				// izzat - 20131218 calculate key and income START
								
				$sql1 = "SELECT mu.id, mu.email, mu.url_name, mu.phone_no, mu.user_MGM_id, mu.fullname, mu.identity_no, mu.address1, mu.address2, mu.mobile_no, us.start_month, us.end_month
						FROM mgm_users mu
						LEFT JOIN user_subscription us 
						ON mu.id = us.user_id
						WHERE mu.sponsor_id = '$user_MGM_id'
						AND (us.end_month = (SELECT MAX(us1.end_month) 
											FROM user_subscription us1
											WHERE us1.user_id = mu.id)
							OR us.end_month IS NULL )
						GROUP BY mu.id 
						ORDER BY mu.id";

				$result1 = mysql_query($sql1);

				$sql2 = "SELECT mu.id, mu.email, mu.url_name, mu.phone_no, mu.user_MGM_id, mu.fullname, mu.identity_no, mu.address1, mu.address2, mu.mobile_no, us.start_month, us.end_month
						FROM mgm_users mu
						LEFT JOIN user_subscription us 
						ON mu.id = us.user_id
						WHERE mu.user_MGM_id = '$user_MGM_id'
						AND (us.end_month = (SELECT MAX(us1.end_month) 
											FROM user_subscription us1
											WHERE us1.user_id = mu.id)
							OR us.end_month IS NULL )";

				$result2 = mysql_query($sql2);
				
				$allInfo = new retreaveInfo(0, 0, " ");
				$allchild = array();
				$childrend = 0;
				$totalChild = 0;
				$totalActive = 0;
				$totalKey = 0;
				$totalIncome=0;
				$totalActiveKey = 0;
				$totalActiveIncome=0;
				$key = 2;

				while($row = mysql_fetch_array($result1))
				{
					$retreaveInfo = new retreaveInfo(0, 0, " ");
					$retreaveInfo->setTrees("<ul><li>".$row['url_name']."<ul>");
					$allInfo = calculateIncome($row['user_MGM_id'], $retreaveInfo);
					$totalChild = $totalChild + intval($allInfo->getTotalChild());
					$totalActive = $totalActive + intval($allInfo->getTotalActive());
				// 	echo $row['user_MGM_id']."--> totalChild -- > ".$totalChild."<p>";
					$statusActive = checkStatus($row['start_month'],$row['end_month']);
					$obj=new CustomerInfo($row['id'], $row['email'], $row['url_name'], $row['phone_no'], $row['user_MGM_id'], $row['fullname'], $row['identity_no'], $row['address1'], $row['address2'], $row['mobile_no'], $row['start_month'], $allInfo->getTotalChild(), $allInfo->getTotalActive(), $statusActive);
					$allchild[] = $obj;
					$dummy = $allInfo->getTrees()."</ul></li></ul>";
					$allInfo->setTrees($dummy);
					
					// count totalKey and totalIncome
					if($key != 0)
					{
						$totalKey = $totalKey + intval($allInfo->getTotalChild());
						$totalActiveKey = $totalKey + intval($allInfo->getTotalActive());
						$key--;
					}
					else
					{
						$totalIncome = $totalIncome + intval($allInfo->getTotalChild());
						$totalActiveIncome = $totalIncome + intval($allInfo->getTotalActive());
					}
				}

				$userProfileDB = mysql_fetch_array($result2);
				$parentStatus = checkStatus($userProfileDB['start_month'],$userProfileDB['end_month']);
				$userProfile=new CustomerInfo($userProfileDB['id'], $userProfileDB['email'], $userProfileDB['url_name'], $userProfileDB['phone_no'], $userProfileDB['user_MGM_id'], $userProfileDB['fullname'], $userProfileDB['identity_no'], $userProfileDB['address1'], $userProfileDB['address2'], $userProfileDB['mobile_no'], $userProfileDB['start_month'], $totalChild, $totalActive, $parentStatus);

				
				$_SESSION['user_profile'] = $userProfile;
				$_SESSION['user_key_income_list'] = $allchild;
				$_SESSION['user_total_key'] = $totalKey;
				$_SESSION['user_total_income'] = $totalIncome;
				$_SESSION['user_total_active_key'] = $totalActiveKey;
				$_SESSION['user_total_active_income'] = $totalActiveIncome;
				
				// izzat - 20131218 calculate key and income END
				
			} else {
				$loginresult = 2; // pwd salah
			}
		}
		else {
			$loginresult = 3; //takde user ID
		}
		echo $loginresult;
		
	
?>